package com.zb.jsp.action;

import java.io.File;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.zb.jsp.service.advert.AdvertService;
import com.zb.jsp.utils.CopyFile;
import com.zb.jsp.utils.GetExtension;
import com.zb.jsp.utils.IdsLink;
import com.zb.jsp.vo.advert.AdvertVo;

public class AdvertAction extends ActionSupport {

	// 实例化advertVo对象来接受页面信息
	private AdvertVo advertVo;
	// 用于查询广告信息的vo
	private AdvertVo advertVoQry;
	// 用于删除的id数组
	private String[] selectId;
	// 用于判断注册还是修改 的标志符
	String butFlgVal;

	// 实例化一个AdvertService
	AdvertService advertService = new AdvertService();
	private List<AdvertVo> advertVoList;
	// 定义上传文件
	File uploadAdvert;
	String uploadAdvertFileName;
	String uploadAdvertContentType;

	String uploadFolderPath = ServletActionContext.getServletContext()
			.getRealPath("/uploadAdvertFile");
	File uploadFolder = new File(uploadFolderPath);

	/**
	 * 重写execute方法将页面信息保存到数据库
	 */
	public String execute() {
		if (!uploadFolder.isDirectory()) {
			uploadFolder.mkdirs();
		}
		uploadAdvertFileName = new Date().getTime()
				+ GetExtension.getExtension(uploadAdvertFileName);

		// 目标文件的文件路径
		String destPath = uploadFolderPath + "/" + uploadAdvertFileName;
		// 根据目标文件路径创建目标文件
		File dest = new File(destPath);

		CopyFile.copy(uploadAdvert, dest);
		advertVo.setAdvertPath(uploadAdvertFileName);

		// 调用advertService中的方法将vo中 的信息保存到数据库
		advertService.addAdvert(advertVo);
		return "addAdvertSuccess";
	}

	/**
	 * 查询广告信息
	 * 
	 * @return 查询成功标志
	 */
	public String searchAdvert() {

		System.out.println(advertVoQry);
		advertVoList = advertService.findAdvertVoListByAdvertVoQry(advertVoQry);
		return "searchAdvertSuccess";

	}

	/**
	 * 根据id删除广告的信息
	 * 
	 * @return 执行广告查询方法
	 */
	public String delAdvert() {
		// 将页面获取到的id数组拼接成字串
		String ids = IdsLink.idsLink(selectId);
		// 调用service方法删除选中的记录
		advertService.delAdvert(ids);
		// 返回查询页面
		System.out.println("删除成功！");
		return searchAdvert();

	}

	/**
	 * 用于修改前将信息显示在修改页面的查询
	 * 
	 * @return 返回更新成功标志
	 */
	public String initUptAdvert() {
		// 根据ID查询广告位信息
		advertVo = advertService.findTblAdvertById(advertVo.getId());
		butFlgVal = "upt";
		return "initUptAdvertSuccess";
	}

	/**
	 * 更新广告位信息
	 * 
	 * @return
	 */
	public String uptAdvert() {
    
		if (uploadAdvert != null) {
			if (!uploadFolder.isDirectory()) {
				uploadFolder.mkdirs();
			}
			uploadAdvertFileName = new Date().getTime()
					+ GetExtension.getExtension(uploadAdvertFileName);

			// 目标文件的文件路径
			String destPath = uploadFolderPath + "/" + uploadAdvertFileName;
			// 根据目标文件路径创建目标文件
			File dest = new File(destPath);

			CopyFile.copy(uploadAdvert, dest);

			advertVo.setAdvertPath(uploadAdvertFileName);
		}

		// 更新广告位信息到数据库中
		advertService.uptAdvert(advertVo);
		return "uptAdvertSuccess";
	}

	// 变量及对象的get、set方法
	public AdvertVo getAdvertVo() {
		return advertVo;
	}
    
	public void setAdvertVo(AdvertVo advertVo) {
		this.advertVo = advertVo;
	}

	public File getUploadAdvert() {
		return uploadAdvert;
	}

	public void setUploadAdvert(File uploadAdvert) {
		this.uploadAdvert = uploadAdvert;
	}

	public String getUploadAdvertFileName() {
		return uploadAdvertFileName;
	}

	public void setUploadAdvertFileName(String uploadAdvertFileName) {
		this.uploadAdvertFileName = uploadAdvertFileName;
	}

	public String getUploadAdvertContentType() {
		return uploadAdvertContentType;
	}

	public void setUploadAdvertContentType(String uploadAdvertContentType) {
		this.uploadAdvertContentType = uploadAdvertContentType;
	}

	public AdvertVo getAdvertVoQry() {
		return advertVoQry;
	}

	public void setAdvertVoQry(AdvertVo advertVoQry) {
		this.advertVoQry = advertVoQry;
	}

	public List<AdvertVo> getAdvertVoList() {
		return advertVoList;
	}

	public void setAdvertVoList(List<AdvertVo> advertVoList) {
		this.advertVoList = advertVoList;
	}

	public String[] getSelectId() {
		return selectId;
	}

	public void setSelectId(String[] selectId) {
		this.selectId = selectId;
	}

	public String getButFlgVal() {
		return butFlgVal;
	}

	public void setButFlgVal(String butFlgVal) {
		this.butFlgVal = butFlgVal;
	}
}
